// FillUp.h: interface for the CFillUp class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_FILLUP_H__F0FD1F83_6F88_4AA0_949C_0AC334C8BF46__INCLUDED_)
#define AFX_FILLUP_H__F0FD1F83_6F88_4AA0_949C_0AC334C8BF46__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "DrawingNode.h"
#include <stack>

// CFillUp은 모든 작업이 OnLButtonDown()에서 끝나기 때문에,
// OnLButtonUp(), OnMouseMove()가 없다.
class CFillUp : public CDrawingNode  
{
	// CFillUp 클래스에서만 사용하는 하위 구조체
	struct PointElement
	{
		enum DIRECTION { NONE = 0, BELLOW = -1, UPPER = +1 };
		
		CPoint point;
		int left;
		int right;
		int prev_left;
		int prev_right;
		int flag;
		bool bInit;
		
		void Constructor(CPoint _point)
		{
			point = _point;
			left = right = _point.x;
			bInit = false;
		}
		
		PointElement(CPoint _point)
		{
			Constructor(_point);
			prev_left = 0;
			prev_right = 0;
			flag = NONE;
		}
		
		PointElement(CPoint _point, int _prev_left, int _prev_right, int _flag)
		{
			Constructor(_point);
			prev_left = _prev_left;
			prev_right = _prev_right;
			flag = _flag;
		}
	};
	
	CBitmap m_bmpBuffer;		// 색칠한 것 저장해놓을 용도
	CBitmap* m_pbmpOldBuffer;	// DC의 비트맵 복구용.
	CDC m_BufferDC;				// 메모리 DC
	
	CBitmap m_bmpShadow;		// 그림자 저장해놓을 용도
	CBitmap* m_pbmpOldShadow;	// 그림자DC 비트맵 복구용
	CDC m_ShadowDC;				// 그림자용 메모리 DC
	
	CPoint m_ptClicked;		// 색칠하려고 클릭한 위치
	CSize m_sizeCanvas;		// 캔바스 사이즈
	
public:
	void OnLButtonDown(CDC *pDC, UINT nFlags, CPoint point, CPoint scrolled);
	void Draw(CDC* pDC);
	CFillUp();
	virtual ~CFillUp();
	
private:
	bool CheckLine(CDC* pCacheDC, COLORREF colorClicked, stack<PointElement>& pointsStack, int y, int& left, int& right, PointElement::DIRECTION dir);
	void DrawLineLeft2Right(CDC *pDC, CPoint scrolled, int left, int right, int y);
	int ScanRight(CDC *pDC, COLORREF color, CPoint point);
	int ScanLeft(CDC *pDC, COLORREF color, CPoint point);
	bool IsValidatePixel(CDC* pBufferDC, CDC *pCacheDC, CPoint point, COLORREF color);
	void FillUpFirstTime(CDC *pDC, CDC *pCacheDC, CPoint scrolled);
};

#endif // !defined(AFX_FILLUP_H__F0FD1F83_6F88_4AA0_949C_0AC334C8BF46__INCLUDED_)
